<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table border="1">
    <caption>商品列表</caption>
    <tr>
        <th>商品标题</th>
        <th>商品价格</th>
        <th>商品库存</th>
    </tr>
</table>
<script>
    //1. 实例化一个空对象    Person.java    Person p = new Person();    p.name="xxx";
    let p1 = {};
    //动态添加属性或方法 Java语言不支持动态添加属性和方法
    p1.name = "张三";
    p1.age = 18;
    p1.run = function () {
        console.log(this.name+":"+this.age);
    }
    //调用方法
    p1.run();
    //2.实例化一个自带属性和方法的对象
    let p2 = {name:"李四",age:20,run:function () {
            console.log(this.name+":"+this.age);
        }};
    p2.run();
    //3. 通过JS对象封装服务器响应回来的数据
    let productArr = [{title:"华为遥遥领先Mate 60 pro",price:8000,num:0},
        {title:"小米14 pro",price:6000,num:10000},
        {title:"苹果15",price:10000,num:50}];
    //得到table
    let table = document.querySelector("table");
    //把模拟从服务器请求到的商品数据展示到页面中
    for (let p of productArr) {
        //创建tr 和 td
        let tr = document.createElement("tr");
        let titleTd = document.createElement("td");
        let priceTd = document.createElement("td");
        let numTd = document.createElement("td");
        titleTd.innerText = p.title;
        priceTd.innerText = p.price;
        numTd.innerText = p.num;
        //把td装进tr
        tr.append(titleTd,priceTd,numTd);
        //把tr装进table
        table.append(tr);
    }










</script>
</body>
</html>